package com.ruoyi.wms.service;

import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.domain.bo.employee.EmployeePointsDetailsBo;
import com.ruoyi.wms.domain.vo.EmployeePointsDetailsVo;
import com.ruoyi.wms.domain.entity.employee.EmployeePointsDetails;
import com.ruoyi.wms.mapper.EmployeePointsDetailsMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 员工积分明细Service业务层处理
 *
 * @author 邓滔滔
 * @date 2024-12-09
 */
@RequiredArgsConstructor
@Service
public class EmployeePointsDetailsService {

    private final EmployeePointsDetailsMapper employeePointsDetailsMapper;

    /**
     * 查询员工积分明细
     */
    public EmployeePointsDetailsVo queryById(Long id){
        return employeePointsDetailsMapper.selectVoById(id);
    }

    /**
     * 查询员工积分明细列表
     */
    public TableDataInfo<EmployeePointsDetailsVo> queryPageList(EmployeePointsDetailsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmployeePointsDetails> lqw = buildQueryWrapper(bo);
        Page<EmployeePointsDetailsVo> result = employeePointsDetailsMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询员工积分明细列表
     */
    public List<EmployeePointsDetailsVo> queryList(EmployeePointsDetailsBo bo) {
        LambdaQueryWrapper<EmployeePointsDetails> lqw = buildQueryWrapper(bo);
        return employeePointsDetailsMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmployeePointsDetails> buildQueryWrapper(EmployeePointsDetailsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmployeePointsDetails> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEmployeeId() != null, EmployeePointsDetails::getEmployeeId, bo.getEmployeeId());
        lqw.between(params.get("beginChangeDate") != null && params.get("endChangeDate") != null,
            EmployeePointsDetails::getChangeDate ,params.get("beginChangeDate"), params.get("endChangeDate"));
        return lqw;
    }

    /**
     * 新增员工积分明细
     */
    public void insertByBo(EmployeePointsDetailsBo bo) {
        EmployeePointsDetails add = MapstructUtils.convert(bo, EmployeePointsDetails.class);
        employeePointsDetailsMapper.insert(add);
    }

    /**
     * 修改员工积分明细
     */
    public void updateByBo(EmployeePointsDetailsBo bo) {
        EmployeePointsDetails update = MapstructUtils.convert(bo, EmployeePointsDetails.class);
        employeePointsDetailsMapper.updateById(update);
    }

    /**
     * 批量删除员工积分明细
     */
    public void deleteByIds(Collection<Long> ids) {
        employeePointsDetailsMapper.deleteBatchIds(ids);
    }
}
